<?php
	include ('config.php');
	include ('utility.php');
	include ('twitter.php');
	include ('twitteroauth.php');
	include ('oauth_lib.php');
	require_once('Autolink.php');
	
	function verify($name, $password, $remember) {
		$t = new twitter($name, $password);
		$user = $t->verify();
		if (!isset($user->error) && isset($user->name)) {
			$time = $remember ? time()+3600*24*365 : 0; 
			setEncryptCookie('twitese_name', $user->screen_name, $time, '/');
			setEncryptCookie('twitese_account', $name, $time, '/');
			setEncryptCookie('twitese_pw', $password, $time, '/');
			setcookie('friends_count', $user->friends_count, $time, '/');
			setcookie('statuses_count', $user->statuses_count, $time, '/');
			setcookie('followers_count', $user->followers_count, $time, '/');
			setcookie('imgurl', $user->profile_image_url, $time, '/');
			setcookie('name', $user->name, $time, '/');
			return true;
		} else {
			return $user;
		}
	}
	
	function saveStyle($headerBg, $bodyBg, $sideBg, $sideNavBg, $linkColor, $linkHColor, $wordColor, $border, $line) {
		$time = time() + 3600*24*30;
		setcookie("headerBg", $headerBg, $time);
		setcookie("bodyBg", $bodyBg, $time);
		setcookie("sideBg", $sideBg, $time);
		setcookie("sideNavBg", $sideNavBg, $time);
		setcookie("linkColor", $linkColor, $time);
		setcookie("linkHColor", $linkHColor, $time);
		setcookie("wordColor", $wordColor, $time);
		setcookie("border", $border, $time);
		setcookie("line", $line, $time);
	}

	function resetStyle() {
		delCookie("headerBg");
		delCookie("bodyBg");
		delCookie("sideBg");
		delCookie("sideNavBg");
		delCookie("linkColor");
		delCookie("linkHColor");
		delCookie("wordColor");
		delCookie("border");
		delCookie("line");
	}
	
	function getColor($name, $default) {
		if (getCookie($name)) return getCookie($name);
		else return $default;
	}
		
	function setUpdateCookie($value) {
		setcookie('update_status', $value);
	}
	
	function getUpdateCookie() {
		if ( isset($_COOKIE['update_status']) ) {
			$update_status = $_COOKIE['update_status'];
			setcookie('update_status', '', time()-300);
			return $update_status;
		} else {
			return null;
		}
	}
	
	function formatText($text) {
		$autolinker = new Twitter_Autolink();
		$text = $autolinker->autolink($text);
		return $text;
	}
	
	function formatDate($date, $is_raw = false) {
		date_default_timezone_set('Asia/Chongqing'); 
		$differ = time() - strtotime($date);

		if ($is_raw) {
			$dateFormated = date('Y-m-d H:i:s', strtotime($date)); 
		} else {
			if ($differ < 0) $differ = 0;
			if ($differ < 60) {
				$dateFormated = ceil($differ) . "秒前";
			} else if ($differ < 3600) {
				$dateFormated = ceil($differ/60) . "分钟前";
			} else if ($differ < 3600*24) {
				$dateFormated = "约" . ceil($differ/3600) . "小时前";
			} else {
				$dateFormated = date('Y-m-d H:i:s', strtotime($date)); 
			}
		}
		
		return $dateFormated;
	}
	
	function imglyUpload( $image ) {
		$postdata = array('status' => 'test', 'pic' => "@$image", 'username' => getEncryptCookie('twitese_account'), 'password' => getEncryptCookie('twitese_pw'), 'source' => WB_AKEY);
	    $request = 'http://api.t.sina.com.cn/statuses/upload.xml';
	    $result = objectifyXml( processCurl( $request, $postdata ) );
		print_r($result);
		return true;
	}
	
	function getTwitter() {
		if(isOAuth()){
			$access_token = isset($_SESSION['access_token']) ? $_SESSION['access_token'] : null;
			$oauth_token = $access_token ? $access_token['oauth_token'] : $_COOKIE['oauth_token'];
			$oauth_token_secret = $access_token ? $access_token['oauth_token_secret'] : $_COOKIE['oauth_token_secret'];
			$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
			return $oauth;
		}
		$type = function_exists('json_decode') ? 'json': 'xml';
		return new twitter(getEncryptCookie('twitese_account'), getEncryptCookie('twitese_pw'), $type);
	}
	
	function isLogin(){
		return isBasicAuth() || isOAuth();
	}
	
	function isBasicAuth() {
		return getEncryptCookie('twitese_account') && getEncryptCookie('twitese_pw');
	}
	
	function isOAuth() {
		if(isset($_SESSION['login_status']) && $_SESSION['login_status'] == 'verified'){
			return true;
		}
		else{
			return getCookie('twitese_name') && getCookie('twitese_userid') && getCookie('oauth_token') && getCookie('oauth_token_secret');
		}
	}
	
	/***Template***/
	function tpTimeline($statuses, $argsArr=false) {
		if (isset($argsArr['id'])) {
			$output = '<ol class="timeline" id="' . $argsArr['id'] . '">';
		} else {
			$output = '<ol class="timeline">';
		}
		
		foreach ($statuses as $status) {
			$is_rt = isset($status->retweeted_status);
			
			$username = getEncryptCookie('twitese_name');
			
			$status_id = $status->id;
			$user = $status->user;
			$date = formatDate($status->created_at);
			$rawdate = formatDate($status->created_at, true);
			$text = formatText($status->text);
			$rawtext = $status->text;
			
			if ($is_rt) {
				$rttext = formatText($status->retweeted_status->text);
				$rtuser = $status->retweeted_status->user;
				$rtid = $status->retweeted_status->id;
			}
			
			if(!isset($argsArr['is_mention']) && (strpos($rawtext, "@$username") > -1)) {
				$output .= "<li class=\"mention\">";
			} else {
				$output .= "<li>";
			}
			
			$output .= "
					<span class=\"status_author\">
						<a href=\"user.php?id=$user->screen_name\" target=\"_blank\"><img src=\"$user->profile_image_url\" title=\"$user->screen_name\" /></a>
					</span>
					<span class=\"status_body\">
						<span class=\"status_id\">$status_id</span>
						<span class=\"status_word\"><a class=\"user_name\" href=\"user.php?id=$user->screen_name\" target=\"_blank\">$user->screen_name</a> $text </span>
						"; 
						
			if (isset($status->thumbnail_pic)) {
			$output .= "
			<div class=\"thumb_pic\">
				<a target=\"_blank\" href=\"$status->original_pic\" atl=\"image\">
				<img src=\"$status->thumbnail_pic\">
				</a>
			</div>
			";
			}
			
			if ($is_rt) {
			$output .= "
					<div class=\"inline_rt\">
						<span class=\"status_body inline_rt_body\">
							<input type=\"hidden\" class=\"rt_id\" value=\"$rtid\" />
							<span class=\"status_word\"><a class=\"user_name\" href=\"user.php?id=$rtuser->screen_name\" target=\"_blank\">@$rtuser->screen_name</a>: $rttext </span>
							
						</span>
						<div class=\"clear\"></div>";
			if (isset($status->retweeted_status->thumbnail_pic)) {
			$output .= "
						<div class=\"thumb_pic\">
							<a target=\"_blank\" href=\"{$status->retweeted_status->original_pic}\" atl=\"image\">
							<img src=\"{$status->retweeted_status->thumbnail_pic}\">
							</a>
						</div>
			";
			}
			$output .= "
					</div>";
			}
			
			$output .= "<span class=\"status_info\">";
			if (!isset($argsArr['hide_replie'])) $output .= "<a class=\"replie_btn\" href=\"a_reply.php?id=$status->id\">回复</a>";
			if (!isset($argsArr['hide_comment'])) $output .= "<a class=\"comment_btn\" href=\"status.php?id=$status->id\">评论</a>";
			if (!isset($argsArr['hide_rt']) && $user->screen_name != $username) $output .= "<a class=\"rt_btn\" href=\"a_rt.php?id=$status->id\">转发</a>";
			if (!isset($argsArr['hide_favor'])) $output .= "<a class=\"favor_btn\" href=\"a_favor.php?id=$status->id\">收藏</a>";
			if ($user->screen_name == $username || isset($argsArr['show_del'])) $output .= "<a class=\"delete_btn\" href=\"a_del.php?id=$status->id&t=s\">删除</a>";
			if (!isset($argsArr['is_comment']) && $status->in_reply_to_status_id) $output .= "<span class=\"in_reply_to\"> <a href=\"status.php?id=$status->in_reply_to_status_id \">对 $status->in_reply_to_screen_name 的回复</a></span>";
			
			$output .= "		
							<span class=\"source\">通过 $status->source</span>
							<span class=\"date\" title=\"$rawdate\"><a href=\"status.php?id=$status->id\" target=\"_blank\">$date</a></span>
					    </span>
					</span>";
			$output .= "</li>";
		}
		$output .= "</ol>";
		
		echo $output;
		
	}
	
	function tpEmpty(){
		echo "<div class=\"empty\">此页无内容</div>";
	}
	
	function testResult($result) {
		$url = urlencode('http://' . $_SERVER ['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
		if ((!is_array($result) && !is_object($result))|| !$result || isset($result->errors)) {
			//header("location: error.php?url=$url");
		}
		if ($result == 'noconnect') {
			header("location: error.php?type=1&url=$url");
		} 
		if ($result == 'limit') {
			header("location: error.php?type=2&url=$url");
		} 
	}
	
	function testReferer() {
		$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
		$port = $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443 ? ':'.$_SERVER['SERVER_PORT'] : '';
		$HOST = $scheme . '://' . $_SERVER['HTTP_HOST'] . $port;
		return strpos($_SERVER['HTTP_REFERER'],$HOST) === 0 ? true : false;
	}
?>